//
//  LSPubulishMatchViewController.m
//  LookScore
//
//  Created by wws on 2018/5/8.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSPubulishMatchViewController.h"
#import "LSPublishModel.h"
#import "LSPubulishTopicCell.h"
#import "LSPublishSectionReusableView.h"
#import "LSPubulishMatchBottomView.h"
@interface LSPubulishMatchViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSMutableArray * todayModelArray;
@property (nonatomic, strong) NSMutableArray * yesterdayModelArray;
@property (nonatomic, strong) NSMutableArray * tomorrowModelArray;
@property (nonatomic, assign) NSInteger sectionCount;
@property (nonatomic, assign) float footerViewH;
@property (nonatomic, assign) NSInteger selectMatchId;
@end

@implementation LSPubulishMatchViewController{
    
    UIButton *tmpBtn;
}

- (NSMutableArray *)todayModelArray{
    
    if (!_todayModelArray) {
        _todayModelArray = @[].mutableCopy;
    }
    return _todayModelArray;
}

- (NSMutableArray *)yesterdayModelArray{
    
    if (!_yesterdayModelArray) {
        _yesterdayModelArray = @[].mutableCopy;
    }
    return _yesterdayModelArray;
}

- (NSMutableArray *)tomorrowModelArray{
    if (!_tomorrowModelArray) {
        _tomorrowModelArray = @[].mutableCopy;
    }
    return _tomorrowModelArray;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing=0;
        flowLayout.minimumInteritemSpacing=0;
        flowLayout.itemSize=CGSizeMake(LSScreenWidth/2,LSAutoVlaue(50, LSScreenWidth));
        _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0,0, LSScreenWidth,LSScreenHeight -64-65) collectionViewLayout:flowLayout];
        //        _collectionView.userInteractionEnabled=YES;
//        flowLayout.headerReferenceSize = CGSizeMake(LSScreenWidth, 50);
//        flowLayout.footerReferenceSize = CGSizeMake(LSScreenWidth, LSAutoVlaue(50, LSScreenWidth));
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
    }
    return _collectionView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    _sectionCount = 1;
    _footerViewH = 50;
    self.title = @"赛事筛选";
    self.view.backgroundColor = LSTitleWhiteColor;
    [self.collectionView registerClass:[LSPublishSectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"LSPubulishTopicCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([LSPubulishTopicCell class])];
    [self.collectionView registerClass:[LSPubulishMatchBottomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
    [self.view addSubview:self.collectionView];
    [self loadData];
    [self loadData1];
    [self createBottomView];
}
- (void)createBottomView{
    
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, LSScreenHeight-64-65, LSScreenWidth, 65)];
    bgView.backgroundColor = LSTitleWhiteColor;
    [self.view addSubview:bgView];
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 0.5)];
    lineView.backgroundColor = LSLineGrayColor;
    [bgView addSubview:lineView];
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(14, 11, LSScreenWidth-28, 39)];
    btn.backgroundColor = LSButtonColor;
    [btn setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    btn.titleLabel.font = LSFontSize15;
    [btn addTarget:self action:@selector(sureBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:btn];
    
    
}

- (void)loadData{
    
    [LSHttpEnclosureTool GET:LSMatchTodayInTopic parameters:nil resBlock:^(id responseObject) {
        if (responseObject) {
            for (NSDictionary * dict in responseObject[@"TodayMatchList"]) {
                LSPublishModel * model = [LSPublishModel mj_objectWithKeyValues:dict];
                [self.todayModelArray addObject:model];
            }
            MyLog(@"%@",responseObject);
        }
        
        [self.collectionView reloadData];
    }];
}

- (void)loadData1{
    
    
    [LSHttpEnclosureTool GET:LSMatchReccentInTopic parameters:nil resBlock:^(id responseObject) {
        if (responseObject) {
            for (NSDictionary * dict in responseObject[@"YesTodayMatchList"]) {
                LSPublishModel * model = [LSPublishModel mj_objectWithKeyValues:dict];
                [self.yesterdayModelArray addObject:model];
            }
            for (NSDictionary * dict in responseObject[@"TomorrowMatchList"]) {
                LSPublishModel * model = [LSPublishModel mj_objectWithKeyValues:dict];
                [self.tomorrowModelArray addObject:model];
            }
        }
        
        [self.collectionView reloadData];
    }];
}


#pragma mark--collentionDelegate & dataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return _sectionCount;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.todayModelArray.count;
    }else if (section == 1){
        
        return self.yesterdayModelArray.count;
    }
    return self.tomorrowModelArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LSPubulishTopicCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LSPubulishTopicCell class]) forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"LSPubulishTopicCell" owner:self options:nil].firstObject;
    }
    if (indexPath.section == 0) {
       cell.model = self.todayModelArray[indexPath.row];
    }else if (indexPath.section == 1){
        
        cell.model = self.yesterdayModelArray[indexPath.row];
    }else{
        cell.model = self.tomorrowModelArray[indexPath.row];
    }
    cell.section = indexPath.section;
    cell.block = ^(UIButton *btn){
        [self btnClick:btn withIndexRow:indexPath];
    };
    cell.selectedBackgroundView = [UIView new];
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *supplementaryView;
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
       LSPublishSectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            headerView.titleLabel.text = @"今天";
        }else if (indexPath.section == 1){
            headerView.titleLabel.text = @"昨天";
            
        }else{
             headerView.titleLabel.text = @"明天";
        }
        supplementaryView = headerView;
        
    }else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
//        if (indexPath.section == 0) {
            LSPubulishMatchBottomView * footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerView" forIndexPath:indexPath];
            footer.block = ^(UIButton *sender) {
                _sectionCount = 3;
                _footerViewH = 0;
                [self.collectionView reloadData];
            };
            supplementaryView = footer;
//        }
    
       
    }else{
        
        supplementaryView = [UICollectionReusableView new];
    }
    
    return supplementaryView;

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(LSScreenWidth, 50);
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
        if (section == 0) {
           return CGSizeMake(LSScreenWidth, _footerViewH);
        }else{
            return CGSizeMake(LSScreenWidth, 0);
        }
}


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
//    if (section == 1) {
//       return CGSizeMake(LSScreenWidth, 50);
//    }else{
//        return CGSizeMake(LSScreenWidth, 0);
//    }
//
//
//}

- (void)btnClick:(UIButton *)sender withIndexRow:(NSIndexPath *)indexPath{
    tmpBtn.selected = NO;
    LSPublishModel * model;
    if (indexPath.section == 0) {
        [tmpBtn setBackgroundColor:LSTitleWhiteColor];
        [sender setTitleColor:LSTitleWhiteColor forState:UIControlStateSelected];
        [sender setBackgroundColor:Color(255,170,0)];
        sender.layer.borderColor = Color(255,170,0).CGColor;
        model = self.todayModelArray[indexPath.row];
    }else if (indexPath.section == 1){
        [tmpBtn setBackgroundColor:Color(210,210,210)];
        [sender setTitleColor:LSTitleWhiteColor forState:UIControlStateSelected];
        [sender setBackgroundColor:Color(255,170,0)];
        sender.layer.borderColor = Color(255,170,0).CGColor;
        model = self.yesterdayModelArray[indexPath.row];
    }else{
        [tmpBtn setBackgroundColor:LSTitleWhiteColor];
        [sender setTitleColor:LSTitleWhiteColor forState:UIControlStateSelected];
        [sender setBackgroundColor:Color(255,170,0)];
        sender.layer.borderColor = Color(255,170,0).CGColor;
        model = self.tomorrowModelArray[indexPath.row];
    }
    tmpBtn = sender;
    sender.selected = YES;

    _selectMatchId = model.MatchId;
    
}
- (void)sureBtnClicked:(UIButton *)sender{
    
    if (_selectMatchId != 0) {
        if (self.block) {
            self.block(_selectMatchId);
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
